/*
 * Copyright (c) 2006-2021, Halo Team
 *
 * SPDX-License-Identifier: GPLv3
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-19     Eywen       the first version
 */
#include "M23.h"
#include "../gcode_file.h"

/* 运动线程主函数入口，从ringbuff中取出任务给硬件定时器提供执行的任务  */
static bool thread_M23(const char *filename)
{
    char *str;
    extern gcode_handle_t gcode_handle;
    gcode_t gcode_tmp;
    debug_info("M23 %d", sizeof(gcode_exp1));
    for(int i = 0; i < sizeof(gcode_exp1)/sizeof(char *); i++){
        str = gcode_exp1[i];
        if(true == parse_gcode_str(&gcode_tmp, str)){
            //continue;
            while(gcode_handle.ops->task_full(&gcode_handle) == true){
                rt_thread_yield();
            }

            gcode_handle.ops->add_gcode(&gcode_handle, &gcode_tmp);
        }
    }
    return true;
}


bool gcode_handle_M23(gcode_t *gcode, bool instantly, fdm_printer_t *fdm_printer)
{
    /* 创建 serial 线程 */
    static rt_thread_t thread = NULL;

    if(thread && (thread->stat != 0x4)){
        return true;
    }

    thread = rt_thread_create("M23", thread_M23, RT_NULL, 1024, 10, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL){
        rt_thread_startup(thread);
    }
    else{
        return false;
    }


    return true;
}
